import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_base/utils/loading/loading_utils.dart';

class CommonLargeTextField extends StatefulWidget {
  final int? maxLength;
  final MaxLengthEnforcement? maxLengthEnforcement;
  final String? hintText;
  final bool readOnly;
  final bool autofocus;
  final TextStyle? style;
  final TextStyle? hintStyle;
  final TextEditingController controller;
  final void Function(String)? onChanged;
  final void Function(String)? onSubmitted;
  final TextInputAction? textInputAction;
  final Decoration? decoration;
  final EdgeInsetsGeometry? padding;
  final int? maxLines;
  final int? minLines;

  const CommonLargeTextField({
    super.key,
    this.maxLength,
    required this.controller,
    this.style,
    this.hintStyle,
    this.autofocus = false,
    this.readOnly = false,
    this.maxLengthEnforcement,
    this.hintText,
    this.textInputAction,
    this.onChanged,
    this.onSubmitted,
    this.decoration,
    this.padding,
    this.maxLines,
    this.minLines,
  });

  @override
  State<CommonLargeTextField> createState() => _CommonLargeTextFieldState();
}

class _CommonLargeTextFieldState extends State<CommonLargeTextField> {
  List<TextInputFormatter> list = [];

  int textCount = 0;

  @override
  void initState() {
    super.initState();
    list.clear();
    widget.controller.addListener(addListenerTextController);
  }

  @override
  void dispose() {
    super.dispose();
    widget.controller.removeListener(addListenerTextController);
  }

  addListenerTextController() {
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: widget.padding ?? EdgeInsets.symmetric(horizontal: 16.w),
      decoration: widget.decoration,
      child: Column(
        children: [
          TextField(
            style: widget.style ?? TextStyle(color: Colors.white, fontSize: 12.sp),
            textAlignVertical: TextAlignVertical.center,
            maxLines: widget.maxLines ?? 4,
            minLines: widget.minLines ?? 4,
            textInputAction: widget.textInputAction,
            readOnly: widget.readOnly,
            maxLength: widget.maxLength,
            onChanged: widget.onChanged,
            onSubmitted: (text) {
              widget.onSubmitted?.call(text);
            },
            autofocus: widget.autofocus,
            inputFormatters: list,
            maxLengthEnforcement: widget.maxLengthEnforcement ?? MaxLengthEnforcement.enforced,
            buildCounter: (context, {required currentLength, maxLength, required bool isFocused}) {
              if (widget.maxLength != null && currentLength >= (widget.maxLength ?? 0) && isFocused) {
                LoadingUtils.showToast("已达到最大字数");
              }
              return null;
            },
            decoration: InputDecoration(
              isCollapsed: true,
              contentPadding: const EdgeInsets.all(0),
              border: const OutlineInputBorder(borderSide: BorderSide.none),
              labelStyle: const TextStyle(color: Colors.white),
              hintText: widget.hintText,
              hintStyle: widget.hintStyle ??
                  TextStyle(
                    color: const Color(0x99FFFFFF),
                    fontSize: 12.sp,
                  ),
            ),
            controller: widget.controller,
          ),
          // 4.verticalSpace,
          Visibility(
            visible: widget.maxLength != null,
            child: Align(
              alignment: Alignment.centerRight,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    '${widget.controller.text.characters.length}/',
                    style: TextStyle(
                      fontSize: 10.sp,
                      color: const Color(0xFF999999),
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  Text(
                    widget.maxLength.toString(),
                    style: TextStyle(
                      fontSize: 10.sp,
                      color: const Color(0xFF999999),
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ],
              ),
            ).marginOnly(top: 4.w),
          )
        ],
      ),
    );
  }
}
